﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using QLMB.Models;
using QLMB.ViewModels;
using System.Web.UI.WebControls;

namespace QLMB.Controllers
{
    public class BookController : Controller
    {
        public IBookTicketRepository Repository { get; set; }

        public BookController()
            : this(new BookTicketRepository())
        {
        }

        public BookController(IBookTicketRepository repository)
        {
            Repository = repository;
        }

        [HttpGet]
        public ActionResult GetPlaces()
        {
            InfoFlight temp = new InfoFlight();
            temp.Ngaydi = DateTime.Now.Date;
            temp.Ngayve = DateTime.Now.AddDays(1).Date;
            Repository.HamKiemTraHuyVe();
            return View(temp);
        }

        [HttpPost]
        public ActionResult GetPlaces(InfoFlight info)
        {

            return RedirectToAction("ListFight", "Book",
                new
                {
                    Sanbaydi = info.Sanbaydi,
                    Sanbayden = info.Sanbayden,
                    Ngaydi = info.Ngaydi,
                    Ngayve = info.Ngayve,
                    Loaive = info.Loaive,
                    SoNguoiLon = info.SoNguoiLon,
                    SoTreEm = info.SoTreEm,
                    time = 0,
                    LoaiDVdi = info.Loaidichvudi,
                    LoaiDVden = info.Loaidichvuve,
                    MaCBdi = info.MaChuyenBayDi,
                    MaCBden = info.MaChuyenBayVe
                });
        }

        [HttpGet]
        public ActionResult ListFight(string Sanbaydi, string Sanbayden, DateTime Ngaydi, DateTime Ngayve, string Loaive, int SoNguoiLon, int SoTreEm, int time, string LoaiDVdi, string LoaiDVden, string MaCBdi, string MaCBden)
        {
            IDictionary<DateTime, Decimal> GiaTuan = new Dictionary<DateTime, Decimal>();
            InfoFlight info = new InfoFlight();
            ViewBag.DSDV = Repository.LayDSCacDichVu();
            if (time == 0)
            {
                ViewBag.NGAYDI = Ngaydi;
                ViewBag.NGAYVE = Ngayve;
                ViewBag.TUADE = "CHON LUOT DI";
                info.Loaidichvuve = LoaiDVden;
                info.MaChuyenBayVe = MaCBden;
                var dscb = Repository.LayDSChuyenBay(Ngaydi, Sanbaydi, Sanbayden);
                if (dscb.Count() != 0)
                {
                    ViewBag.DSCB = dscb;
                    info.MaChuyenBayDi = ((IEnumerable<CHUYENBAY>)dscb).First().MaCB;
                    info.Loaidichvudi = Repository.LayDSCacDichVu(info.MaChuyenBayDi, info.SoTreEm + info.SoNguoiLon).First().MaLoaiDV;
                }

                for (int i = -3; i < 4; i++)
                {
                    DateTime temp = Ngaydi.AddDays(i);
                    GiaTuan.Add(temp, Repository.TimGiaThapNhatTrongNgay(temp, Sanbaydi, Sanbayden));
                }
                ViewBag.GiaHienTai = GiaTuan[Ngaydi];
            }
            else
            {
                ViewBag.NGAYDI = Ngayve;
                ViewBag.NGAYVE = Ngaydi;
                ViewBag.TUADE = "CHON LUOT VE";
                var dscb = Repository.LayDSChuyenBay(Ngayve, Sanbayden, Sanbaydi);
                info.MaChuyenBayDi = MaCBdi;
                info.Loaidichvudi = LoaiDVdi;
                if (dscb.Count() != 0)
                {
                    ViewBag.DSCB = dscb;
                    info.MaChuyenBayVe = ((IEnumerable<CHUYENBAY>)ViewBag.DSCB).First().MaCB;
                    info.Loaidichvuve = Repository.LayDSCacDichVu(info.MaChuyenBayVe, info.SoTreEm + info.SoNguoiLon).First().MaLoaiDV;
                }

                for (int i = -3; i < 4; i++)
                {
                    DateTime temp = Ngayve.AddDays(i);
                    GiaTuan.Add(temp, Repository.TimGiaThapNhatTrongNgay(temp, Sanbayden, Sanbaydi));
                }
                ViewBag.GiaHienTai = GiaTuan[Ngayve];
            }
            ViewBag.TIME = time;
            ViewBag.GiaTuan = GiaTuan;


            info.Sanbaydi = Sanbaydi;
            info.Sanbayden = Sanbayden;
            info.Ngaydi = Ngaydi;
            info.Ngayve = Ngayve;
            info.Loaive = Loaive;
            info.SoNguoiLon = SoNguoiLon;
            info.SoTreEm = SoTreEm;
            info.time = time;
            TempData["info"] = info;
            return View(info);
        }

        [HttpPost]
        public ActionResult ListFight(InfoFlight infotemp)
        {
            InfoFlight info = TempData["info"] as InfoFlight;
            if(info.time == 0)
            {
                if(info.MaChuyenBayDi != null)
                {
                    if (info.Loaive == "Khu hoi")
                    {
                        return RedirectToAction("ListFight", "Book",
                        new
                        {
                            Sanbaydi = info.Sanbaydi,
                            Sanbayden = info.Sanbayden,
                            Ngaydi = info.Ngaydi,
                            Ngayve = info.Ngayve,
                            Loaive = info.Loaive,
                            SoNguoiLon = info.SoNguoiLon,
                            SoTreEm = info.SoTreEm,
                            time = 1,
                            LoaiDVdi = infotemp.Loaidichvudi,
                            LoaiDVden = infotemp.Loaidichvuve,
                            MaCBdi = infotemp.MaChuyenBayDi,
                            MaCBden = infotemp.MaChuyenBayVe
                        });
                    }
                    else
                    {
                        info.MaChuyenBayDi = infotemp.MaChuyenBayDi;
                        info.Loaidichvudi = infotemp.Loaidichvudi;
                        return RedirectToAction("ConfirmTicket", "Book", info);
                    }
                }
                else
                {
                    TempData["info"] = info;
                    return RedirectToAction("GetBestFlight", "Book");
                }
            }
            else
            {
                if(info.MaChuyenBayVe == null)
                {
                    TempData["info"] = info;
                    return RedirectToAction("GetBestFlight", "Book", info);
                }
                info.Loaidichvuve = infotemp.Loaidichvuve;
                info.MaChuyenBayVe = infotemp.MaChuyenBayVe;

                return RedirectToAction("ConfirmTicket", "Book", info);
            }
        }

        [HttpGet]
        public ActionResult GetBestFlight()
        {
            IDictionary<DateTime, CHUYENBAY> GiaThang = new Dictionary<DateTime, CHUYENBAY>();
            InfoFlight info = TempData["info"] as InfoFlight;
            DateTime now = DateTime.Now;
            if (info.time == 0)
            {
                for (int i = 0; i < 30; i++)
                {
                    DateTime temp = now.AddDays(i);
                    GiaThang.Add(temp, Repository.TimCBGiaThapNhat(temp, info.Sanbaydi, info.Sanbayden));
                }
            }
            else
            {
                for (int i = 0; i < 30; i++)
                {
                    DateTime temp = now.AddDays(i);
                    GiaThang.Add(temp, Repository.TimCBGiaThapNhat(temp, info.Sanbayden, info.Sanbaydi));
                }
            }
            if (info.time == 0)
            {
                ViewBag.NOIDI = info.Sanbaydi;
                ViewBag.NOIDEN = info.Sanbayden;
            }
            else
            {
                ViewBag.NOIDI = info.Sanbayden;
                ViewBag.NOIDEN = info.Sanbaydi;
            }
            ViewBag.NGAY = now;
            ViewBag.GIATHANG = GiaThang;
            return View(info);
        }

        [HttpGet]
        public ActionResult ConfirmTicket(InfoFlight info, int def = 1)
        {
            ViewBag.CBDi = Repository.LayChuyenBay(info.MaChuyenBayDi)  ;
            ViewBag.LoaiDVDi = Repository.LayLoaiDichVu(info.Loaidichvudi);
            ViewBag.ThueSbDi = ViewBag.CBDi.DUONGBAY.SANBAY.THUE.PhuThu;
            Decimal giadi = (ViewBag.CBDi.DUONGBAY.GiaCoBan - ViewBag.CBDi.DUONGBAY.GiaCoBan*ViewBag.CBDi.KhuyenMai/100);
            ViewBag.TienVeDi = giadi + ViewBag.LoaiDVDi.TongTien + giadi*5/100;
            ViewBag.TienThueNguoiLonDi = info.SoNguoiLon * ViewBag.ThueSbDi;
            ViewBag.TienThueTreEmDi = info.SoTreEm * ViewBag.ThueSbDi / 2;
            ViewBag.ThueVAT = ViewBag.TienVeDi * (info.SoNguoiLon + info.SoTreEm) / 10;
            info.TongTien = (info.SoNguoiLon+ info.SoTreEm)*ViewBag.TienVeDi + ViewBag.TienThueNguoiLonDi + ViewBag.TienThueTreEmDi;
            if (info.Loaive == "Khu hoi")
            {
                ViewBag.CBVe = Repository.LayChuyenBay(info.MaChuyenBayVe);
                ViewBag.LoaiDVVe = Repository.LayLoaiDichVu(info.Loaidichvuve);
                ViewBag.ThueSbVe = ViewBag.CBDi.DUONGBAY.SANBAY1.THUE.PhuThu;
                Decimal giave = ViewBag.CBVe.DUONGBAY.GiaCoBan - ViewBag.CBVe.DUONGBAY.GiaCoBan*ViewBag.CBVe.KhuyenMai/100;
                ViewBag.TienVeVe = giave + ViewBag.LoaiDVVe.TongTien + giave * 5 / 100;
                ViewBag.TienThueNguoiLonVe = info.SoNguoiLon * ViewBag.ThueSbVe;
                ViewBag.TienThueTreEmVe = info.SoTreEm * ViewBag.ThueSbVe / 2;
                ViewBag.ThueVAT += ViewBag.TienVeVe * (info.SoNguoiLon + info.SoTreEm) / 10;
                info.TongTien = info.TongTien + (info.SoNguoiLon+ info.SoTreEm)*ViewBag.TienVeVe + ViewBag.TienThueNguoiLonVe + ViewBag.TienThueTreEmVe;
            }
            info.TongTien += ViewBag.ThueVAT;
            TempData["info"] = info;
            return View(info);
        }

        [HttpPost]
        public ActionResult ConfirmTicket()
        {
            InfoFlight info = TempData["info"] as InfoFlight;
            return RedirectToAction("InfoBooker","Book",info);
        }

        [HttpGet]
        public ActionResult InfoBooker(InfoFlight info, int dif = 1)
        {
            info.khdd = "-1";
            List<ListItem> items = new List<ListItem>(info.SoNguoiLon);
            items.Add(new ListItem { Text = "Chọn hành khách...", Value = "-1" });
            for(int j=0; j < info.SoNguoiLon; j++)
            {
                items.Add(new ListItem
                {
                    Text = "Hành khách " + (j + 1).ToString(),
                    Value = j.ToString()
                });
            }
            ViewBag.SList = new SelectList(items,"Value","Text");
            info.dsHK = new List<InfoTraveller>();
            int l = info.SoNguoiLon + info.SoTreEm;
            for (int i = 0; i < l; i++)
                info.dsHK.Add(new InfoTraveller());
            info.Nguoidat = new InfoUser();
            info.Nguoidat.NgaySinh = new DateTime(DateTime.Now.Year - 18,1,1);
            ViewBag.l = l;
            return View(info);
        }

        [HttpPost]
        public ActionResult InfoBooker(InfoFlight info)
        {
            TempData["info"] = info;
            return RedirectToAction("Purchase", "Book", info);
        }

        [HttpGet]
        public ActionResult Purchase()
        {
            ViewBag.URL = Url.Action("Finish", 1);
            InfoFlight info = TempData["info"] as InfoFlight;
            ViewBag.Sotien = (long)info.TongTien;
            ViewBag.CBDi = Repository.LayChuyenBay(info.MaChuyenBayDi);
            ViewBag.LoaiDVDi = Repository.LayLoaiDichVu(info.Loaidichvudi);
            if (info.Loaive == "Khu hoi")
            {
                ViewBag.CBVe = Repository.LayChuyenBay(info.MaChuyenBayVe);
                ViewBag.LoaiDVVe = Repository.LayLoaiDichVu(info.Loaidichvuve);
            }
            TempData["info"] = info;
            ViewBag.HKTT = 0;
            if(info.Nguoidat.MaHK != null)
            {
                HANHKHACHTHANTHIET hktt = Repository.LayHKTT(info.Nguoidat.MaHK);
                if(hktt != null)
                {
                    ViewBag.HKTT = 1;
                    ViewBag.GiamGia = hktt.LOAIHK.GiamGia;
                    ViewBag.LoaiHK = hktt.LOAIHK.TenLoai;
                    ViewBag.TienGiam = (info.TongTien*ViewBag.GiamGia/100);
                    ViewBag.TienCuoiCung = (info.TongTien - ViewBag.TienGiam);
                    info.TongTien = ViewBag.TienCuoiCung;
                }
            }
            return View(info);
        }

        [HttpPost]
        public ActionResult Purchase(InfoFlight infotemp)
        {
            InfoFlight info = TempData["info"] as InfoFlight;
            TempData["info"] = info;
            Repository.DatVe(info);
            return RedirectToAction("Finish","Book");
        }

        [HttpGet]
        public ActionResult Finish(int thanhtoan = 0)
        {
            ViewBag.ThanhToan = thanhtoan;
            InfoFlight info = TempData["info"] as InfoFlight;
            return View();
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetListArriveAirport(string id)
        {
            InfoFlight temp = new InfoFlight();
            var sbden = temp.LayDSSBDen(id);
            return Json(sbden, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetListServices(string id, int sldv)
        {
            List<ListItem> list = new List<ListItem>();
            IEnumerable<LOAIDICHVU> dsdv = Repository.LayDSCacDichVu(id,sldv);
            foreach (LOAIDICHVU item in dsdv)
            {
                list.Add(new ListItem { Value = item.MaLoaiDV, Text = "" });
            }

            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public PartialViewResult _FeeInfo()
        {
            return PartialView();
        }

        [HttpGet]
        public PartialViewResult _DetailFlight(string macb)
        {
            CHUYENBAY cb = Repository.LayChuyenBay(macb);
            ViewBag.dssb = Repository.LayHanhTrinh(macb);
            return PartialView(cb);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult SearchInfo(string id)
        {
            HANHKHACH temp = Repository.LayHanhKhach(id);
            if (temp != null)
            {
                if (temp.HANHKHACHTHANTHIET == null)
                {
                    var hk = new
                    {
                        Quyen = 1,
                        Ho = temp.Ho,
                        Ten = temp.Ten,
                        SDT = temp.SoDTDiDong,
                        MaHK = temp.MaHK,
                        DanhTinh = temp.DanhTinh,
                        GioiTinh = temp.GioiTinh,
                        NgaySinh = string.Format("{0:MM/dd/yyyy}",temp.NgaySinh),
                        SDTB = temp.SoDTBan,
                        Email = temp.Email,
                        Fax = temp.SoFax,
                    };
                    return Json(hk, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    var hk = new
                    {
                        Quyen = 0,
                        Ho = temp.Ho,
                        Ten = temp.Ten,
                        SDT = temp.SoDTDiDong,
                        MaHK = temp.MaHK,
                        DanhTinh = temp.DanhTinh,
                        GioiTinh = temp.GioiTinh,
                        NgaySinh = string.Format("{0:MM/dd/yyyy}", temp.NgaySinh),
                        SDTB = temp.SoDTBan,
                        Email = temp.Email,
                        Fax = temp.SoFax, 
                    };
                    return Json(hk, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return null;
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult CheckUser(string name, string pass)
        {
            HANHKHACHTHANTHIET temp = Repository.LayHKTT(name, pass);
            if(temp!=null)
            {
                var hk = new
                {
                    Count = 1,
                    Ho = temp.HANHKHACH.Ho,
                    Ten = temp.HANHKHACH.Ten,
                    DanhTinh = temp.HANHKHACH.DanhTinh,
                    GioiTinh = temp.HANHKHACH.GioiTinh,
                    NgaySinh = string.Format("{0:MM/dd/yyyy}",temp.HANHKHACH.NgaySinh),
                    SoDD = temp.HANHKHACH.SoDTDiDong,
                    SoDTB = temp.HANHKHACH.SoDTBan,
                    Email = temp.HANHKHACH.Email,
                    Fax = temp.HANHKHACH.SoFax,
                    CMND = temp.HANHKHACH.CMND,
                    MaHK = temp.MaHK,
                };
                return Json(hk, JsonRequestBehavior.AllowGet);
            }
            var hk1 = new { Count = 0 };
            return Json(hk1, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult CheckSameUser(string cmnd, string macb)
        {
            if(Repository.KiemtraHK(cmnd,macb))
            {
                var json = new{check = true};
                return Json(json, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var json = new { check = false };
                return Json(json, JsonRequestBehavior.AllowGet);
            }
            
        }

        

    }
}
